<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label" />
        <el-input
          v-model="query.nameCn"
          clearable
          placeholder="中文名称"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label" />
        <el-input
          v-model="query.nameEn"
          clearable
          placeholder="英文名称"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="800px"
      >
        <el-form ref="form" :model="form" :rules="rules" label-width="100px" inline style="height: 300px">
          <el-col :span="12">
            <el-form-item label="中文名称" prop="nameCn">
              <el-input v-model="form.nameCn" style="width: 220px;" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="英文名称" prop="nameEn">
              <el-input v-model="form.nameEn" style="width: 220px;" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序">
              <el-input-number
                v-model.number="form.sort"
                :min="0"
                :max="999999"
                controls-position="right"
                style="width: 178px;"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否展示">
              <el-radio v-for="item in dict.true_false" :key="item.id" v-model="form.showStatus" :label="item.dictKey">
                {{ item.dictValue }}
              </el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="品牌故事">
              <el-input v-model="form.brandStory" :rows="2" type="textarea" style="width: 600px;" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="专区大图">
              <el-upload
                ref="my-upload"
                :on-success="(res,file)=>{handleSuccess('bigPic',res,file)}"
                :on-error="handleError"
                :headers="headers"
                :action="imagesUploadApi"
                :data="type"
                :show-file-list="false"
              >
                <img v-if="form.bigPic" :src="baseApi+form.bigPic" class="avatar" width="150px" height="150px">
                <el-button v-else size="small" type="primary">点击上传</el-button>
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="品牌logo">
              <el-upload
                ref="my-upload"
                :on-success="(res,file)=>{handleSuccess('logo',res,file)}"
                :on-error="handleError"
                :headers="headers"
                :action="imagesUploadApi"
                :data="type"
                :show-file-list="false"
              >
                <img v-if="form.logo" :src="baseApi+form.logo" class="avatar" width="150px" height="150px">
                <el-button v-else size="small" type="primary">点击上传</el-button>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        row-key="id"
        :data="crud.data"
        style="width: 100%;"
        stripe
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="nameCn" label="中文名称">
          <template slot-scope="scope">
            <div>{{ scope.row.nameCn }}</div>
            <div>{{ scope.row.nameEn }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="bigPic" label="专区大图">
          <template slot-scope="scope">
            <img :src="baseApi + scope.row.bigPic" alt="" style="max-height: 90px">
          </template>
        </el-table-column>
        <el-table-column prop="logo" label="品牌logo">
          <template slot-scope="scope">
            <img :src="baseApi + scope.row.logo" style="max-height: 90px" alt="">
          </template>
        </el-table-column>
        <el-table-column prop="brandStory" label="品牌故事" :show-overflow-tooltip="true" />
        <el-table-column prop="showStatus" label="是否展示" />
        <el-table-column prop="sort" label="排序" />
        <el-table-column v-if="checkPer(['admin','brand:edit','brand:del'])" label="操作" width="150px">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudBrand from '@/api/pms/brand'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { getToken } from '@/utils/auth'
import { mapGetters } from 'vuex'

const defaultForm = {
  id: null,
  nameCn: null,
  nameEn: null,
  bigPic: null,
  logo: null,
  productCommentCount: 0,
  productCount: 0,
  brandStory: null,
  showStatus: false,
  sort: 0
}
export default {
  name: 'Brand',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['true_false'],
  cruds() {
    return CRUD({ title: '品牌管理', url: 'brand/page', sort: 'id:desc', crudMethod: { ...crudBrand }})
  },
  data() {
    return {
      type: { type: 3 },
      headers: {
        'Authorization': getToken()
      },
      permission: {
        add: ['admin', 'brand:add'],
        edit: ['admin', 'brand:edit'],
        del: ['admin', 'brand:del']
      },
      rules: {
        nameCn: [
          { required: true, message: '中文名称不能为空', trigger: 'blur' }
        ],
        nameEn: [
          { required: true, message: '英文名称不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'nameCn', display_name: '中文名称' },
        { key: 'nameEn', display_name: '英文名称' }
      ]
    }
  },
  computed: {
    ...mapGetters(['imagesUploadApi', 'baseApi'])
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    handleSuccess(item, res, file) {
      if (res.code === 0) {
        this.form[item] = res.data.url
      } else {
        this.$message.error('图片上传失败,请重新上传')
      }
    },
    handleError(res) {
      this.$message.error('图片上传失败,请重新上传')
    }
  }
}
</script>

<style scoped>

</style>
